package cn.stylefeng.guns.modular.business.custom.convert;

import cn.stylefeng.guns.core.enums.PaymentTypeEnum;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author jzy
 * @Date 2021/10/23
 **/
public class PaymentTypeConvert implements Converter<String> {
    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null;
    }

    @Override
    public CellData convertToExcelData(String s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (s.length() > 2) {
            String paymentStr = s.substring(1, s.length()-1);
            List<String> list = new ArrayList<>();
            for (String s1 : paymentStr.split(",")) {
                list.add(PaymentTypeEnum.getValue(Integer.valueOf(s1.trim())));
            }
            return new CellData(StringUtils.join(list, ","));
        } else {
            return new CellData();
        }
    }
}
